package com.sherry.uitils;

import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by swl on 2021/6/2.
 */
public class CrawlerUtil {

    public static Map<String, String> crawler(String wikiAddress) {
        Connection connection = Jsoup.connect(wikiAddress);
        connection.header("cookie", "JSESSIONID=0B6DB6EC2390B8D564B0D97F3006BA3A");

        Response response = null;
        try {
            response = connection.execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        System.out.println(response.parse().html());

        Document document = null;
        try {
            document = response.parse();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Element titleElement = document.getElementById("title-text");
        String title = titleElement.text();
        System.out.println("接口文档标题:"+title);

        Elements wikiContent = document.getElementsByClass("wiki-content");
        Element content = wikiContent.get(0).child(1).child(0).child(0);
        String url = content.child(1).child(1).text();
        System.out.println("接口url:"+url);

        String body = document.html();
//        System.out.println(body);

        String requestProto = "无";
        if (body.contains("Request{")) {
            int i = body.indexOf("Request{") + 7;
            int j = body.indexOf("message ") + 8;
            requestProto = body.substring(j, i);
        } else if (body.contains("Request {")) {
            int i = body.indexOf("Request {") + 8;
            int j = body.indexOf("message ") + 8;
            requestProto = body.substring(j, i);
        }
        System.out.println("接口requestProto:"+requestProto);

        String responseProto = "无";
        if (body.contains("Response{")) {
            int i = body.indexOf("Response{") + 8;
            int j = i - 100;
            responseProto = body.substring(j, i);
            responseProto = responseProto.substring(responseProto.indexOf("message ") + 8);
        } else if (body.contains("Response {")) {
            int i = body.indexOf("Response {") + 9;
            int j = i - 100;
            responseProto = body.substring(j, i);
        }
        System.out.println("接口responseProto:"+responseProto);

        String type = "";
        StringBuilder file = new StringBuilder();
        if (url.length() > 0) {
            type = url.substring(url.indexOf("/api") + 5);
            type = type.substring(0, type.indexOf("/"));
            System.out.println("接口类型:"+type);

            String fileName = url.substring(url.lastIndexOf("/") + 1);
            String[] tempFile = fileName.split("_");
            for (String e : tempFile) {
                char[] cs = e.toCharArray();
                cs[0] -= 32;
                String temp = String.valueOf(cs);
                file.append(temp);
            }
            System.out.println("测试用例名称:"+ file);
        }


        Map<String, String> map = new HashMap<String, String>();
        map.put("description", title);
        map.put("url", url);
        map.put("request", requestProto);
        map.put("response", responseProto);
        map.put("type", type);
        map.put("fileName", file.toString());
        return map;
    }
}